{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\Geraldus Wilsen\\Documents\\Portfolio\\KnowledgeGraphLLM\\venv\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import json\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from langchain_community.graphs import Neo4jGraph\n",
    "from langchain.chains import GraphCypherQAChain\n",
    "from langchain_google_genai import ChatGoogleGenerativeAI\n",
    "from langchain_community.llms import HuggingFaceHub\n",
    "load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Gemini (https://aistudio.google.com/app/apikey)\n",
    "gemini_api = os.getenv(\"GEMINI_API\")\n",
    "\n",
    "# Hugging Face (if we want to use open source LLM)\n",
    "hf_api = os.getenv(\"HF_API\")\n",
    "\n",
    "# Neo4j \n",
    "neo4j_url = os.getenv(\"NEO4J_CONNECTION_URL\")\n",
    "neo4j_user = os.getenv(\"NEO4J_USER\")\n",
    "neo4j_password = os.getenv(\"NEO4J_PASSWORD\")\n",
    "\n",
    "# https://api.python.langchain.com/en/latest/graphs/langchain_community.graphs.neo4j_graph.Neo4jGraph.html\n",
    "graph = Neo4jGraph(neo4j_url,neo4j_user,neo4j_password)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Data Pre-processing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Thanks to Manish Kumar, for providing this dataset in Kaggle (https://www.kaggle.com/datasets/manishkumar7432698/linkedinuserprofiles?select=LinkedIn+people+profiles+datasets.csv)!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>timestamp</th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>city</th>\n",
       "      <th>country_code</th>\n",
       "      <th>region</th>\n",
       "      <th>current_company:company_id</th>\n",
       "      <th>current_company:name</th>\n",
       "      <th>position</th>\n",
       "      <th>following</th>\n",
       "      <th>...</th>\n",
       "      <th>people_also_viewed</th>\n",
       "      <th>educations_details</th>\n",
       "      <th>education</th>\n",
       "      <th>avatar</th>\n",
       "      <th>languages</th>\n",
       "      <th>certifications</th>\n",
       "      <th>recommendations</th>\n",
       "      <th>recommendations_count</th>\n",
       "      <th>volunteer_experience</th>\n",
       "      <th>сourses</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-01-10</td>\n",
       "      <td>catherinemcilkenny</td>\n",
       "      <td>Catherine Fitzpatrick (McIlkenny), B.A</td>\n",
       "      <td>Canada</td>\n",
       "      <td>CA</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Snr Business Analyst at Emploi et Développemen...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>[{\"profile_link\":\"https://ca.linkedin.com/in/l...</td>\n",
       "      <td>Queen's University Belfast</td>\n",
       "      <td>[{\"degree\":\"Bachelor of Arts (B.A.) Honours\",\"...</td>\n",
       "      <td>https://media.licdn.com/dms/image/C4E03AQEcz_j...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2022-12-17</td>\n",
       "      <td>margot-bon-51a04624</td>\n",
       "      <td>Margot Bon</td>\n",
       "      <td>The Randstad, Netherlands</td>\n",
       "      <td>NL</td>\n",
       "      <td>EU</td>\n",
       "      <td>gemeente-utrecht</td>\n",
       "      <td>Gemeente Utrecht</td>\n",
       "      <td>Communicatieadviseur Corporate &amp; Strategie Gem...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>[{\"profile_link\":\"https://nl.linkedin.com/in/j...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>[{\"degree\":\"Scrum en Agile werken\",\"end_year\":...</td>\n",
       "      <td>https://static.licdn.com/sc/h/244xhbkr7g40x6bs...</td>\n",
       "      <td>[{\"subtitle\":\"Professional working proficiency...</td>\n",
       "      <td>[{\"meta\":\"Issued Jun 2013\",\"subtitle\":\"Van der...</td>\n",
       "      <td>Menno H. Poort “Ik werk al jaren prettig met M...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>[{\"cause\":\"\",\"duration\":\"Sep 2010 Jul 2020 9 y...</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-05-17</td>\n",
       "      <td>mike-dean-8509a193</td>\n",
       "      <td>Mike Dean</td>\n",
       "      <td>England, United Kingdom</td>\n",
       "      <td>UK</td>\n",
       "      <td>NaN</td>\n",
       "      <td>network-rail</td>\n",
       "      <td>Network Rail</td>\n",
       "      <td>Network Data Manager at Network Rail</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>[{\"profile_link\":\"https://uk.linkedin.com/in/g...</td>\n",
       "      <td>Brighton Polytechnic</td>\n",
       "      <td>[{\"degree\":\"2:2\",\"end_year\":\"1991\",\"field\":\"El...</td>\n",
       "      <td>https://media.licdn.com/dms/image/C4D03AQHLj-Z...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022-05-29</td>\n",
       "      <td>giovanna-panarella-99a0a4167</td>\n",
       "      <td>Giovanna Panarella</td>\n",
       "      <td>Avellino, Campania, Italy</td>\n",
       "      <td>IT</td>\n",
       "      <td>EU</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Freelance</td>\n",
       "      <td>Architetto (Freelance)</td>\n",
       "      <td>500.0</td>\n",
       "      <td>...</td>\n",
       "      <td>[{\"profile_link\":\"https://it.linkedin.com/in/e...</td>\n",
       "      <td>Università di Camerino</td>\n",
       "      <td>[{\"degree\":\"“Corso di aggiornamento profession...</td>\n",
       "      <td>https://media-exp1.licdn.com/dms/image/C4D03AQ...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>[{\"cause\":\"Arts and Culture\",\"duration\":\"Jan 2...</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2022-12-06</td>\n",
       "      <td>steve-latimer-3364327</td>\n",
       "      <td>Steve Latimer</td>\n",
       "      <td>Ontario, Canada</td>\n",
       "      <td>CA</td>\n",
       "      <td>NaN</td>\n",
       "      <td>mid-range-computer-group-inc.</td>\n",
       "      <td>Mid-Range Computer Group Inc.</td>\n",
       "      <td>Senior Account Executive at Mid-Range Computer...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>[{\"profile_link\":\"https://ca.linkedin.com/in/d...</td>\n",
       "      <td>St. Michael's College School</td>\n",
       "      <td>[{\"degree\":\"\",\"end_year\":\"1978\",\"field\":\"\",\"me...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>[{\"meta\":\"Issued Jan 2022 See credential\",\"sub...</td>\n",
       "      <td>Blake Reeves “If I was a customer, I would wan...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    timestamp                            id  \\\n",
       "0  2023-01-10            catherinemcilkenny   \n",
       "1  2022-12-17           margot-bon-51a04624   \n",
       "2  2023-05-17            mike-dean-8509a193   \n",
       "3  2022-05-29  giovanna-panarella-99a0a4167   \n",
       "4  2022-12-06         steve-latimer-3364327   \n",
       "\n",
       "                                     name                       city  \\\n",
       "0  Catherine Fitzpatrick (McIlkenny), B.A                     Canada   \n",
       "1                              Margot Bon  The Randstad, Netherlands   \n",
       "2                               Mike Dean    England, United Kingdom   \n",
       "3                      Giovanna Panarella  Avellino, Campania, Italy   \n",
       "4                           Steve Latimer            Ontario, Canada   \n",
       "\n",
       "  country_code region     current_company:company_id  \\\n",
       "0           CA    NaN                            NaN   \n",
       "1           NL     EU               gemeente-utrecht   \n",
       "2           UK    NaN                   network-rail   \n",
       "3           IT     EU                            NaN   \n",
       "4           CA    NaN  mid-range-computer-group-inc.   \n",
       "\n",
       "            current_company:name  \\\n",
       "0                            NaN   \n",
       "1               Gemeente Utrecht   \n",
       "2                   Network Rail   \n",
       "3                      Freelance   \n",
       "4  Mid-Range Computer Group Inc.   \n",
       "\n",
       "                                            position  following  ...  \\\n",
       "0  Snr Business Analyst at Emploi et Développemen...        NaN  ...   \n",
       "1  Communicatieadviseur Corporate & Strategie Gem...        NaN  ...   \n",
       "2               Network Data Manager at Network Rail        NaN  ...   \n",
       "3                             Architetto (Freelance)      500.0  ...   \n",
       "4  Senior Account Executive at Mid-Range Computer...        NaN  ...   \n",
       "\n",
       "                                  people_also_viewed  \\\n",
       "0  [{\"profile_link\":\"https://ca.linkedin.com/in/l...   \n",
       "1  [{\"profile_link\":\"https://nl.linkedin.com/in/j...   \n",
       "2  [{\"profile_link\":\"https://uk.linkedin.com/in/g...   \n",
       "3  [{\"profile_link\":\"https://it.linkedin.com/in/e...   \n",
       "4  [{\"profile_link\":\"https://ca.linkedin.com/in/d...   \n",
       "\n",
       "             educations_details  \\\n",
       "0    Queen's University Belfast   \n",
       "1                           NaN   \n",
       "2          Brighton Polytechnic   \n",
       "3        Università di Camerino   \n",
       "4  St. Michael's College School   \n",
       "\n",
       "                                           education  \\\n",
       "0  [{\"degree\":\"Bachelor of Arts (B.A.) Honours\",\"...   \n",
       "1  [{\"degree\":\"Scrum en Agile werken\",\"end_year\":...   \n",
       "2  [{\"degree\":\"2:2\",\"end_year\":\"1991\",\"field\":\"El...   \n",
       "3  [{\"degree\":\"“Corso di aggiornamento profession...   \n",
       "4  [{\"degree\":\"\",\"end_year\":\"1978\",\"field\":\"\",\"me...   \n",
       "\n",
       "                                              avatar  \\\n",
       "0  https://media.licdn.com/dms/image/C4E03AQEcz_j...   \n",
       "1  https://static.licdn.com/sc/h/244xhbkr7g40x6bs...   \n",
       "2  https://media.licdn.com/dms/image/C4D03AQHLj-Z...   \n",
       "3  https://media-exp1.licdn.com/dms/image/C4D03AQ...   \n",
       "4                                                NaN   \n",
       "\n",
       "                                           languages  \\\n",
       "0                                                NaN   \n",
       "1  [{\"subtitle\":\"Professional working proficiency...   \n",
       "2                                                NaN   \n",
       "3                                                NaN   \n",
       "4                                                NaN   \n",
       "\n",
       "                                      certifications  \\\n",
       "0                                                NaN   \n",
       "1  [{\"meta\":\"Issued Jun 2013\",\"subtitle\":\"Van der...   \n",
       "2                                                NaN   \n",
       "3                                                NaN   \n",
       "4  [{\"meta\":\"Issued Jan 2022 See credential\",\"sub...   \n",
       "\n",
       "                                     recommendations recommendations_count  \\\n",
       "0                                                NaN                   NaN   \n",
       "1  Menno H. Poort “Ik werk al jaren prettig met M...                   2.0   \n",
       "2                                                NaN                   NaN   \n",
       "3                                                NaN                   NaN   \n",
       "4  Blake Reeves “If I was a customer, I would wan...                   1.0   \n",
       "\n",
       "                                volunteer_experience сourses  \n",
       "0                                                NaN     NaN  \n",
       "1  [{\"cause\":\"\",\"duration\":\"Sep 2010 Jul 2020 9 y...     NaN  \n",
       "2                                                NaN     NaN  \n",
       "3  [{\"cause\":\"Arts and Culture\",\"duration\":\"Jan 2...     NaN  \n",
       "4                                                NaN     NaN  \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('LinkedIn people profiles datasets.csv') \n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>company</th>\n",
       "      <th>education</th>\n",
       "      <th>languages</th>\n",
       "      <th>industry</th>\n",
       "      <th>country</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>paul-lukes-906608134</td>\n",
       "      <td>Paul Lukes</td>\n",
       "      <td>Toolbox Creative</td>\n",
       "      <td>California College of the Arts</td>\n",
       "      <td>English|Czech</td>\n",
       "      <td>Advertising Services</td>\n",
       "      <td>United States</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>roberto-merola-baa923103</td>\n",
       "      <td>Roberto Merola</td>\n",
       "      <td>Capgemini</td>\n",
       "      <td>Université libre de Bruxelles</td>\n",
       "      <td>English|Italian|French|Dutch|German</td>\n",
       "      <td>IT Services and IT Consulting</td>\n",
       "      <td>Belgium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>minju-hong-bsn-rn-1a7801239</td>\n",
       "      <td>Minju Hong, BSN, RN</td>\n",
       "      <td>University of Washington Medical Center</td>\n",
       "      <td>University of Washington School of Nursing</td>\n",
       "      <td>Korean|English</td>\n",
       "      <td>Hospitals and Health Care</td>\n",
       "      <td>United States</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>prateek-chitpur-710a1a12a</td>\n",
       "      <td>Prateek Chitpur</td>\n",
       "      <td>George Mason University</td>\n",
       "      <td>George Mason University Education George Mason...</td>\n",
       "      <td>English|Hindi|Marathi|Kannada|Telugu</td>\n",
       "      <td>Higher Education</td>\n",
       "      <td>United States</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>aadcampos</td>\n",
       "      <td>Alexandre Campos</td>\n",
       "      <td>Serpro - Serviço Federal de Processamento de D...</td>\n",
       "      <td>Unichristus</td>\n",
       "      <td>English</td>\n",
       "      <td>IT Services and IT Consulting</td>\n",
       "      <td>Brazil</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>gareth-reid-75966110b</td>\n",
       "      <td>Gareth Reid</td>\n",
       "      <td>Willis Towers Watson</td>\n",
       "      <td>University of Leicester</td>\n",
       "      <td>English|French|Spanish</td>\n",
       "      <td>Financial Services</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>alaa-el-said-56740659</td>\n",
       "      <td>Alaa El-said</td>\n",
       "      <td>Microsoft</td>\n",
       "      <td>Mansoura University</td>\n",
       "      <td>Arabic|English</td>\n",
       "      <td>Software Development</td>\n",
       "      <td>Saudi Arabia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>bagus-satya-mas</td>\n",
       "      <td>Bagus Satya Mas</td>\n",
       "      <td>Jatis Mobile</td>\n",
       "      <td>Universitas Udayana (UNUD)</td>\n",
       "      <td>Indonesian|English|Japanese</td>\n",
       "      <td>Software Development</td>\n",
       "      <td>Indonesia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>emrecruit</td>\n",
       "      <td>Emily S.</td>\n",
       "      <td>Dignity Health</td>\n",
       "      <td>Ottawa University</td>\n",
       "      <td>Spanish</td>\n",
       "      <td>Hospitals and Health Care</td>\n",
       "      <td>Greater Phoenix Area</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>giteshpatel21</td>\n",
       "      <td>Gitesh Patel</td>\n",
       "      <td>Baptist Health System KY &amp; IN</td>\n",
       "      <td>Sullivan University</td>\n",
       "      <td>English|Hindi|Gujarati</td>\n",
       "      <td>Hospitals and Health Care</td>\n",
       "      <td>United States</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            id                 name  \\\n",
       "0         paul-lukes-906608134           Paul Lukes   \n",
       "1     roberto-merola-baa923103       Roberto Merola   \n",
       "2  minju-hong-bsn-rn-1a7801239  Minju Hong, BSN, RN   \n",
       "3    prateek-chitpur-710a1a12a      Prateek Chitpur   \n",
       "4                    aadcampos     Alexandre Campos   \n",
       "5        gareth-reid-75966110b          Gareth Reid   \n",
       "6        alaa-el-said-56740659         Alaa El-said   \n",
       "7              bagus-satya-mas      Bagus Satya Mas   \n",
       "8                    emrecruit             Emily S.   \n",
       "9                giteshpatel21         Gitesh Patel   \n",
       "\n",
       "                                             company  \\\n",
       "0                                   Toolbox Creative   \n",
       "1                                          Capgemini   \n",
       "2            University of Washington Medical Center   \n",
       "3                            George Mason University   \n",
       "4  Serpro - Serviço Federal de Processamento de D...   \n",
       "5                               Willis Towers Watson   \n",
       "6                                          Microsoft   \n",
       "7                                       Jatis Mobile   \n",
       "8                                     Dignity Health   \n",
       "9                      Baptist Health System KY & IN   \n",
       "\n",
       "                                           education  \\\n",
       "0                     California College of the Arts   \n",
       "1                      Université libre de Bruxelles   \n",
       "2         University of Washington School of Nursing   \n",
       "3  George Mason University Education George Mason...   \n",
       "4                                        Unichristus   \n",
       "5                            University of Leicester   \n",
       "6                                Mansoura University   \n",
       "7                         Universitas Udayana (UNUD)   \n",
       "8                                  Ottawa University   \n",
       "9                                Sullivan University   \n",
       "\n",
       "                              languages                       industry  \\\n",
       "0                         English|Czech           Advertising Services   \n",
       "1   English|Italian|French|Dutch|German  IT Services and IT Consulting   \n",
       "2                        Korean|English      Hospitals and Health Care   \n",
       "3  English|Hindi|Marathi|Kannada|Telugu               Higher Education   \n",
       "4                               English  IT Services and IT Consulting   \n",
       "5                English|French|Spanish             Financial Services   \n",
       "6                        Arabic|English           Software Development   \n",
       "7           Indonesian|English|Japanese           Software Development   \n",
       "8                               Spanish      Hospitals and Health Care   \n",
       "9                English|Hindi|Gujarati      Hospitals and Health Care   \n",
       "\n",
       "                country  \n",
       "0         United States  \n",
       "1               Belgium  \n",
       "2         United States  \n",
       "3         United States  \n",
       "4                Brazil  \n",
       "5        United Kingdom  \n",
       "6          Saudi Arabia  \n",
       "7             Indonesia  \n",
       "8  Greater Phoenix Area  \n",
       "9         United States  "
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def extract_industry(json_str):\n",
    "    try:\n",
    "        data = json.loads(json_str)\n",
    "        return data.get('industry', None)\n",
    "    except json.JSONDecodeError:\n",
    "        return None\n",
    "\n",
    "def extract_languages(json_list):\n",
    "    try:\n",
    "        languages = [entry['title'] for entry in json.loads(json_list)]\n",
    "        return '|'.join(languages)\n",
    "    except: \n",
    "        return None\n",
    "\n",
    "def extract_country(string):\n",
    "    if isinstance(string, str):\n",
    "        elements = string.split(',')\n",
    "        return elements[-1].strip()  \n",
    "    else:\n",
    "        return None\n",
    "\n",
    "df['industry'] = df['current_company'].apply(lambda x: extract_industry(x))\n",
    "df['languages'] = df['languages'].apply(lambda x: extract_languages(x))\n",
    "df['country'] = df['city'].apply(lambda x: extract_country(x))\n",
    "df = df [['id','name','current_company:name','educations_details','languages','industry','country']].dropna()\n",
    "industry_counts = df['industry'].value_counts()\n",
    "df = df[df['industry'].isin(industry_counts[industry_counts > 2].index)].reset_index(drop=True)\n",
    "df = df.rename(columns={'current_company:name': 'company','educations_details':'education'})\n",
    "df.head(10)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Insert data from CSV to Neo4J"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Node properties are the following:\n",
      "\n",
      "Relationship properties are the following:\n",
      "\n",
      "The relationships are the following:\n",
      "\n"
     ]
    }
   ],
   "source": [
    "graph.refresh_schema()\n",
    "print(graph.schema)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "people_query = \"\"\"\n",
    "LOAD CSV WITH HEADERS FROM 'https://raw.githubusercontent.com/projectwilsen/KnowledgeGraphLLM/main/data1.csv'\n",
    "AS row\n",
    "MERGE (person:Person {name: row.name})\n",
    "MERGE (company:Company {name: row.company})\n",
    "MERGE (school:School {name: row.education})\n",
    "MERGE (industry:Industry {name: row.industry})\n",
    "MERGE (country:Country {name: row.country})\n",
    "\n",
    "FOREACH (lang in split(row.languages, '|') | \n",
    "    MERGE (language:Language {name:trim(lang)})\n",
    "    MERGE (person)-[:SPEAKS]->(language))\n",
    "\n",
    "MERGE (person)-[:WORKS_IN]->(company)\n",
    "MERGE (person)-[:LIVES_IN]->(country)\n",
    "MERGE (person)-[:EDUCATED_AT]->(school)\n",
    "MERGE (company)-[:IS_IN]->(industry)\n",
    "\"\"\"\n",
    "\n",
    "graph.query(people_query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Node properties are the following:\n",
      "Person {name: STRING},Company {name: STRING},School {name: STRING},Industry {name: STRING},Country {name: STRING},Language {name: STRING}\n",
      "Relationship properties are the following:\n",
      "\n",
      "The relationships are the following:\n",
      "(:Person)-[:SPEAKS]->(:Language),(:Person)-[:WORKS_IN]->(:Company),(:Person)-[:LIVES_IN]->(:Country),(:Person)-[:EDUCATED_AT]->(:School),(:Company)-[:IS_IN]->(:Industry)\n"
     ]
    }
   ],
   "source": [
    "graph.refresh_schema()\n",
    "print(graph.schema)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. Query through our Knowledge Graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatGoogleGenerativeAI(model=\"gemini-pro\", google_api_key = gemini_api ,temperature = 0)\n",
    "chain = GraphCypherQAChain.from_llm(graph=graph, llm=llm, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (c:Company)-[:IS_IN]->(i:Industry)\n",
      "WHERE i.name = \"Advertising Services\"\n",
      "RETURN c.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[{'c.name': 'Toolbox Creative'}, {'c.name': 'Baked Advertising'}, {'c.name': 'Search Engine People'}]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "Toolbox Creative, Baked Advertising, Search Engine People\n",
      "====== END ====== \n",
      "\n",
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person)-[:EDUCATED_AT]->(s:School {name: \"Simon Fraser University\"})-[:IS_IN]->(i:Industry)<-[:IS_IN]-(c:Company)<-[:WORKS_IN]-(p)\n",
      "RETURN c.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "I do not have that information.\n",
      "====== END ====== \n",
      "\n",
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person {name: \"Paul Lukes\"})-[:WORKS_IN]->(c:Company) RETURN c.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[{'c.name': 'Toolbox Creative'}]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "I don't know the answer.\n",
      "====== END ====== \n",
      "\n",
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person)-[:SPEAKS]->(l:Language {name: \"Vietnamese\"})-[:SPOKEN_IN]->(c:Country {name: \"Canada\"})-[:LIVES_IN]->(p)\n",
      "RETURN p.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "I do not have that information.\n",
      "====== END ====== \n",
      "\n",
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mcypher\n",
      "MATCH (p:Person)-[:SPEAKS]->(l:Language)-[:IS_NATIVE_IN]->(c:Country)\n",
      "WHERE c.name = \"United States\" AND l.name = \"Urdu\"\n",
      "RETURN count(p)\n",
      "\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[{'count(p)': 0}]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "I don't know the answer.\n",
      "====== END ====== \n",
      "\n"
     ]
    }
   ],
   "source": [
    "questions = [\"List all companies in advertising services industry!\",\n",
    "             \"A worker who graduated from Simon Fraser University is currently employed at?\",\n",
    "             \"Where is Paul Lukes working?\",\n",
    "             \"A worker residing in Canada who is proficient in Vietnamese?\",\n",
    "             \"How many worker in United States speak Urdu?\"]\n",
    "for q in questions:\n",
    "    print('====== START ======')\n",
    "    print(chain.invoke(q)['result'])\n",
    "    print('====== END ====== \\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. Prompting Strategies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "examples= [\n",
    "    {\n",
    "        \"question\": \"Which workers speak French?\",\n",
    "        \"query\": \"MATCH (p:Person)-[:SPEAKS]->(l:Language {{name: 'French'}}) RETURN p.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"What industries are workers named Emily associated with?\",\n",
    "        \"query\": \"MATCH (p:Person {{name: 'Emily'}})-[:WORKS_IN]->(c:Company)-[:IS_IN]->(i:Industry) RETURN i.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"Which workers live in Canada and speak German?\",\n",
    "        \"query\": \"MATCH (p:Person)-[:LIVES_IN]->(:Country {{name: 'Canada'}}), (p)-[:SPEAKS]->(:Language {{name: 'German'}}) RETURN p.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"In which countries do workers who speak Spanish live?\",\n",
    "        \"query\": \"MATCH (p:Person)-[:SPEAKS]->(:Language {{name: 'Spanish'}})<-[:SPEAKS]-(worker:Person)-[:LIVES_IN]->(c:Country) RETURN DISTINCT c.name AS Country\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"What companies do workers named John work in?\",\n",
    "        \"query\": \"MATCH (p:Person {{name: 'John'}})-[:WORKS_IN]->(c:Company) RETURN c.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\":\"How many workers in Hospital and Health Care industry able to speak Korea\",\n",
    "        \"query\": \"MATCH (p:Person)-[:WORKS_IN]->(:Company)-[:IS_IN]->(:Industry {{name: 'Hospitals and Health Care'}}),(p)-[:SPEAKS]->(:Language {{name: 'Korean'}}) RETURN COUNT(DISTINCT p) AS NumberOfWorkers\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"What companies are located in the technology industry?\",\n",
    "        \"query\": \"MATCH (c:Company)-[:IS_IN]->(:Industry {{name: 'Technology'}}) RETURN c.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"Where do workers named Alice live?\",\n",
    "        \"query\": \"MATCH (p:Person {{name: 'Alice'}})-[:LIVES_IN]->(c:Country) RETURN c.name\",\n",
    "    },\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate\n",
    "\n",
    "example_prompt = PromptTemplate.from_template(\n",
    "    \"User input: {question}\\nCypher query: {query}\"\n",
    ")\n",
    "prompt = FewShotPromptTemplate(\n",
    "    examples=examples[:3],\n",
    "    example_prompt=example_prompt,\n",
    "    prefix=\"You are a Neo4j expert. Given an input question, create a syntactically correct Cypher query to run.\\n\\nHere is the schema information\\n{schema}.\\n\\nBelow are a number of examples of questions and their corresponding Cypher queries.\",\n",
    "    suffix=\"User input: {question}\\nCypher query: \",\n",
    "    input_variables=[\"question\", \"schema\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are a Neo4j expert. Given an input question, create a syntactically correct Cypher query to run.\n",
      "\n",
      "Here is the schema information\n",
      "foo.\n",
      "\n",
      "Below are a number of examples of questions and their corresponding Cypher queries.\n",
      "\n",
      "User input: Which workers speak French?\n",
      "Cypher query: MATCH (p:Person)-[:SPEAKS]->(l:Language {name: 'French'}) RETURN p.name\n",
      "\n",
      "User input: What industries are workers named Emily associated with?\n",
      "Cypher query: MATCH (p:Person {name: 'Emily'})-[:WORKS_IN]->(c:Company)-[:IS_IN]->(i:Industry) RETURN i.name\n",
      "\n",
      "User input: Which workers live in Canada and speak German?\n",
      "Cypher query: MATCH (p:Person)-[:LIVES_IN]->(:Country {name: 'Canada'}), (p)-[:SPEAKS]->(:Language {name: 'German'}) RETURN p.name\n",
      "\n",
      "User input: Where do Michael work?\n",
      "Cypher query: \n"
     ]
    }
   ],
   "source": [
    "print(prompt.format(question=\"Where do Michael work?\", schema=\"foo\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "chain2 = GraphCypherQAChain.from_llm(graph=graph, llm=llm, cypher_prompt=prompt, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (c:Company)-[:IS_IN]->(i:Industry {name: 'Advertising Services'}) RETURN c.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[{'c.name': 'Toolbox Creative'}, {'c.name': 'Baked Advertising'}, {'c.name': 'Search Engine People'}]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "Toolbox Creative, Baked Advertising, Search Engine People\n",
      "====== END ====== \n",
      "\n",
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person)-[:EDUCATED_AT]->(:School {name: 'Simon Fraser University'}), (p)-[:WORKS_IN]->(c:Company) RETURN c.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[{'c.name': 'Elastic Path'}]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "I do not have that information.\n",
      "====== END ====== \n",
      "\n",
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person {name: 'Paul Lukes'})-[:WORKS_IN]->(c:Company) RETURN c.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[{'c.name': 'Toolbox Creative'}]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "I don't know the answer.\n",
      "====== END ====== \n",
      "\n",
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person)-[:LIVES_IN]->(:Country {name: 'Canada'}), (p)-[:SPEAKS]->(:Language {name: 'Vietnamese'}) RETURN p.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[{'p.name': 'Vitaly Nhuien'}]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "Vitaly Nhuien is a worker residing in Canada who is proficient in Vietnamese.\n",
      "====== END ====== \n",
      "\n",
      "====== START ======\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (p:Person)-[:LIVES_IN]->(:Country {name: 'United States'}), (p)-[:SPEAKS]->(:Language {name: 'Urdu'}) RETURN count(p)\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[{'count(p)': 1}]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "I don't know the answer.\n",
      "====== END ====== \n",
      "\n"
     ]
    }
   ],
   "source": [
    "questions = [\"List all companies in advertising services industry!\",\n",
    "             \"A worker who graduated from Simon Fraser University is currently employed at?\",\n",
    "             \"Where is Paul Lukes working?\",\n",
    "             \"A worker residing in Canada who is proficient in Vietnamese?\",\n",
    "             \"How many worker in United States speak Urdu?\"]\n",
    "for q in questions:\n",
    "    print('====== START ======')\n",
    "    print(chain2.invoke(q)['result'])\n",
    "    print('====== END ====== \\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are a Neo4j expert. Given an input question, create a syntactically correct Cypher query to run.\n",
      "\n",
      "Here is the schema information\n",
      "foo.\n",
      "\n",
      "Below are a number of examples of questions and their corresponding Cypher queries.\n",
      "\n",
      "User input: Which workers speak French?\n",
      "Cypher query: MATCH (p:Person)-[:SPEAKS]->(l:Language {name: 'French'}) RETURN p.name\n",
      "\n",
      "User input: What industries are workers named Emily associated with?\n",
      "Cypher query: MATCH (p:Person {name: 'Emily'})-[:WORKS_IN]->(c:Company)-[:IS_IN]->(i:Industry) RETURN i.name\n",
      "\n",
      "User input: Which workers live in Canada and speak German?\n",
      "Cypher query: MATCH (p:Person)-[:LIVES_IN]->(:Country {name: 'Canada'}), (p)-[:SPEAKS]->(:Language {name: 'German'}) RETURN p.name\n",
      "\n",
      "User input: Where do Michael work?\n",
      "Cypher query: \n"
     ]
    }
   ],
   "source": [
    "print(prompt.format(question=\"Where do Michael work?\", schema=\"foo\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate\n",
    "\n",
    "example_prompt = PromptTemplate.from_template(\n",
    "    \"User input: {question}\\nCypher query: {query}\"\n",
    ")\n",
    "prompt = FewShotPromptTemplate(\n",
    "    examples=examples[:3],\n",
    "    example_prompt=example_prompt,\n",
    "    prefix=\"You are a Neo4j expert. Given an input question, create a syntactically correct Cypher query to run.\\n\\nHere is the schema information\\n{schema}.\\n\\nBelow are a number of examples of questions and their corresponding Cypher queries.\",\n",
    "    suffix=\"User input: {question}\\nCypher query: \",\n",
    "    input_variables=[\"question\", \"schema\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "examples= [\n",
    "    {\n",
    "        \"question\": \"Which workers speak French?\",\n",
    "        \"query\": \"MATCH (p:Person)-[:SPEAKS]->(l:Language {{name: 'French'}}) RETURN p.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"What industries are workers named Emily associated with?\",\n",
    "        \"query\": \"MATCH (p:Person {{name: 'Emily'}})-[:WORKS_IN]->(c:Company)-[:IS_IN]->(i:Industry) RETURN i.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"Which workers live in Canada and speak German?\",\n",
    "        \"query\": \"MATCH (p:Person)-[:LIVES_IN]->(:Country {{name: 'Canada'}}), (p)-[:SPEAKS]->(:Language {{name: 'German'}}) RETURN p.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"In which countries do workers who speak Spanish live?\",\n",
    "        \"query\": \"MATCH (p:Person)-[:SPEAKS]->(:Language {{name: 'Spanish'}})<-[:SPEAKS]-(worker:Person)-[:LIVES_IN]->(c:Country) RETURN DISTINCT c.name AS Country\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"What companies do workers named John work in?\",\n",
    "        \"query\": \"MATCH (p:Person {{name: 'John'}})-[:WORKS_IN]->(c:Company) RETURN c.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\":\"How many workers in Hospital and Health Care industry able to speak Korea\",\n",
    "        \"query\": \"MATCH (p:Person)-[:WORKS_IN]->(:Company)-[:IS_IN]->(:Industry {{name: 'Hospitals and Health Care'}}),(p)-[:SPEAKS]->(:Language {{name: 'Korean'}}) RETURN COUNT(DISTINCT p) AS NumberOfWorkers\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"What companies are located in the technology industry?\",\n",
    "        \"query\": \"MATCH (c:Company)-[:IS_IN]->(:Industry {{name: 'Technology'}}) RETURN c.name\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"Where do workers named Alice live?\",\n",
    "        \"query\": \"MATCH (p:Person {{name: 'Alice'}})-[:LIVES_IN]->(c:Country) RETURN c.name\",\n",
    "    },\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.vectorstores import Neo4jVector\n",
    "from langchain_core.example_selectors import SemanticSimilarityExampleSelector\n",
    "from langchain_community.embeddings import HuggingFaceEmbeddings\n",
    "\n",
    "example_selector = SemanticSimilarityExampleSelector.from_examples(\n",
    "    examples,\n",
    "    HuggingFaceEmbeddings(),\n",
    "    Neo4jVector,\n",
    "    url = neo4j_url,\n",
    "    username = neo4j_user,\n",
    "    password = neo4j_password,\n",
    "    k=3,\n",
    "    input_keys=[\"question\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "example_selector.select_examples({\"question\": \"Where do Michael work?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "dynamic_prompt = FewShotPromptTemplate(\n",
    "    example_selector=example_selector, #previous: examples = examples[:3]\n",
    "    example_prompt=example_prompt,\n",
    "    prefix=\"You are a Neo4j expert. Given an input question, create a syntactically correct Cypher query to run.\\n\\nHere is the schema information\\n{schema}.\\n\\nBelow are a number of examples of questions and their corresponding Cypher queries.\",\n",
    "    suffix=\"User input: {question}\\nCypher query: \",\n",
    "    input_variables=[\"question\", \"schema\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are a Neo4j expert. Given an input question, create a syntactically correct Cypher query to run.\n",
      "\n",
      "Here is the schema information\n",
      "foo.\n",
      "\n",
      "Below are a number of examples of questions and their corresponding Cypher queries.\n",
      "\n",
      "User input: What companies do workers named John work in?\n",
      "Cypher query: MATCH (p:Person {name: 'John'})-[:WORKS_IN]->(c:Company) RETURN c.name\n",
      "\n",
      "User input: Where do workers named Alice live?\n",
      "Cypher query: MATCH (p:Person {name: 'Alice'})-[:LIVES_IN]->(c:Country) RETURN c.name\n",
      "\n",
      "User input: What industries are workers named Emily associated with?\n",
      "Cypher query: MATCH (p:Person {name: 'Emily'})-[:WORKS_IN]->(c:Company)-[:IS_IN]->(i:Industry) RETURN i.name\n",
      "\n",
      "User input: Where do Michael work?\n",
      "Cypher query: \n"
     ]
    }
   ],
   "source": [
    "print(dynamic_prompt.format(question=\"Where do Michael work?\", schema=\"foo\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
